package com.link.server.ws.handle.impl;

import com.link.common.packet.RequestOperate;
import com.link.common.packet.ResponseOperate;
import com.link.common.packet.recv.RegisterPacket;
import com.link.common.packet.resp.SimplePacketResp;
import com.link.database.entity.DBAccount;
import com.link.database.service.DBAccountService;
import com.link.server.ws.handle.HandleComponent;
import com.link.server.ws.handle.Handler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import jakarta.annotation.Resource;

@HandleComponent(RequestOperate.REGISTER)
public class RegisterHandle implements Handler {
    @Resource
    private DBAccountService dbAccountService;
    @Override
    public   void invoke(RequestOperate operate, ByteBuf packet, ChannelHandlerContext ctx) throws  Exception{
        RegisterPacket registerPacket = RegisterPacket.fromBinary(packet);

        DBAccount dbAccount = dbAccountService.selectByUsername(registerPacket.mobile());
        if(dbAccount!=null){
            this.send(ctx, SimplePacketResp.response(ctx,ResponseOperate.REGISTER, (short) 400));
            return ;
        }
        dbAccount=new DBAccount();
        dbAccount.setUsername(registerPacket.mobile());
        dbAccount.setAdult(false);
        dbAccount.setDisabled(false);
        dbAccount.setCreateTime(System.currentTimeMillis());
        dbAccount.setUpdateTime(System.currentTimeMillis());
        dbAccount.setBaseAttackPower(0);
        dbAccount.setExperienceMultiplier(100);
        dbAccount.setDropRate(100);
        dbAccount.setGameDuration(100);
        dbAccount.setPhone(registerPacket.mobile());

         dbAccountService.insert(dbAccount);
         this.send(ctx,SimplePacketResp.response(ctx,ResponseOperate.REAL_NAME_AUTHENTICATION, (short) 404));


    }
}
